package com.jang.utils;

import com.jang.Entity.ResponseEntity;
import com.jang.Entity.cases.ConfigEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ResultHandle {
    /**
     *在json字符串中获取指定key的value
     *  */
    public static String handle(String param,String preResult,int time) {
        String result ="";
        Logger logger = LoggerFactory.getLogger(ResultHandle.class);
        String rule1 =param+"\":(.+?),";
        String rule2=param +"\":(.+?)}";
        Matcher matcher1 = Pattern.compile(rule1).matcher(preResult);
        Matcher matcher2 = Pattern.compile(rule2).matcher(preResult);
        int i = 0;
        try {
            while (matcher1.find()) {
                i++;
                result = matcher1.group(1);
                if (result.contains("}")){
                    result=result.split("}")[0];
                }
                if (i > time) {
                    break;
                }
            }
            }catch (Exception e) {
                logger.error("结果取值异常", e);
            }
            if (result.length()==0) {
                int j = 0;
                try {
                    while (matcher2.find()) {
                        j++;
                        result = matcher2.group(1);
                        if (j > time) {
                            break;
                        }
                    }

                }catch (Exception e)
                { logger.error("结果取值异常", e); }
            }
            return result;
    }
    public static String bodyReplace(String body,String preResult){
        String bodyrule="@(.+?)\"";
        Matcher matcher = Pattern.compile(bodyrule).matcher(body);
        String newbody = body;
        while (matcher.find()){
         //   body = body.replaceAll("@","");
            String ret = matcher.group(1);//获取需要替换的key
            String time= handle(ret,body,1);//获取需要替换key的位置time
            String bodyParam= handle(ret,preResult,Integer.parseInt(time));//从结果中获取第time个key的值
            newbody=  body.replaceAll("\"@"+ret+"\":"+time,"\""+ret+"\":"+bodyParam);

        }

        return newbody;
    }
    public static List<ConfigEntity> handerReplace(List<ConfigEntity> list, String preResult){
        for (int i=0;i<list.size();i++){
            if (list.get(i).getHeader_value().isEmpty() &&preResult.contains("\""+list.get(i).getHeader_key()+"\"")){
                String s =handle(list.get(i).getHeader_key(),preResult,1);
                list.get(i).setHeader_value(s.substring(1,s.length()-1));
            }
        }
        return list;
    }
    /**
     * 处理接口处理结果返回前端
     * */
    public  static  ResponseEntity returnWeb(int result){
        ResponseEntity responseEntity = new ResponseEntity();
        if (result>=0){
            responseEntity.setMess("成功");
            responseEntity.setCode("000");

        }else {
            responseEntity.setMess("失败");
            responseEntity.setCode("009");

        }
    return responseEntity;
    }

}
